<?php

/**
 *    我的物流设置
 *
 *    @author    censhuixing
 *    @time    2014-05-06
 */
class My_shippingApp extends StoreadminbaseApp
{
	function index()
	{
		/* 取得列表数据 */
		$store_id = intval($this->visitor->get('manage_store'));
		$model_shipping =& m('shipping');
		/*   $shippings     = $model_shipping->find(array(
		'conditions'    => 'store_id = ' . $store_id,
		));*/

		$shipping_list = $model_shipping->get_shipping_list($store_id);//物流列表信息

		$this->assign('shippings', $shipping_list);

		$this->import_resource(array(
		'script' => array(
		array(
		'path' => 'dialog/dialog.js',
		'attr' => 'id="dialog_js"',
		),
		array(
		'path' => 'jquery.ui/jquery.ui.js',
		'attr' => '',
		),
		array(
		'path' => 'jquery.plugins/jquery.validate.js',
		'attr' => '',
		),
		array(
		'path' => 'mlselection.js',
		'attr' => '',
		),
		),
		//'style' =>  'jquery.ui/themes/ui-lightness/jquery.ui.css,res:jqtreetable.css',
		));

		/* 当前位置 */
		$this->_curlocal(LANG::get('seller_center'),    'index.php?app=seller',
		LANG::get('my_shipping'), 'index.php?app=my_shipping',
		LANG::get('shipping_list'));

		/* 当前用户中心菜单 */
		$this->_curitem('my_logistics');

		/* 当前所处子菜单 */
		$this->_curmenu('my_logistics');

		$this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('my_shipping'));
		header("Content-Type:text/html;charset=" . CHARSET);
		$this->display('my_shipping.index.html');
	}

	function add()
	{
		if (!IS_POST)
		{
			/* 当前位置 */
			$this->_curlocal('卖家中心',    'index.php?app=seller',
			LANG::get('my_shipping'), 'index.php?app=my_shipping',
			LANG::get('add_shipping'));

			/* 当前用户中心菜单 */
			$this->_curitem('my_shipping');

			/* 当前所处子菜单 */
			$this->_curmenu('add_shipping');
			$this->_assign_form();
			$this->_get_regions();
			$this->assign('cod_regions', array());
			$this->import_resource('mlselection.js, jquery.plugins/jquery.validate.js');
			header("Content-Type:text/html;charset=" . CHARSET);
			$this->display('my_shipping.form.html');
		}
		else
		{

			$data = array(
			'store_id'      => intval($this->visitor->get('manage_store')),
			'shipping_name' => html_script($_POST['shipping_name']),
			'shipping_desc' => html_script($_POST['shipping_desc']),
			'cod_regions'   => html_script($_POST['region_name']),//发货地址
			'compute_mode'  => intval($_POST['sendType'])==0?'by_number':'by_weight',//按件算还是按重量算
			'item'          => floatval($_POST['sendjs']),//默认运费多少件内
			'item_price'    => floatval($_POST['sendy']),//默认运费多少件内多少钱
			'step'          => floatval($_POST['sendj']),//增加多少
			'step_price'    => floatval($_POST['sendyt']),//增加单位内多少钱
			'shipping_area'   => $_POST['logistics'],//地区运费
			//   'enabled'       => $_POST['enabled'],
			//  'sort_order'    => $_POST['sort_order'],
			);

			$shipping_data['store_id'] =  $data['store_id'];
			$shipping_data['shipping_name'] =  $data['shipping_name'];
			$shipping_data['shipping_desc'] =  $data['shipping_desc'];
			$shipping_data['cod_regions'] =  $data['cod_regions'];
			$shipping_data['compute_mode'] =  $data['compute_mode'];

			$model_shipping =& m('shipping');
			if (!($shipping_id = $model_shipping->add($shipping_data)))
			{
				//$this->show_warning($model_shipping->get_error());

				$this->show_message($model_shipping->get_error());
				return;
			}
			else
			{
				$model_shippingarea =& m('shippingarea');
				$shipping_area_id = $model_shippingarea->add($shipping_id,$data);
				$this->show_message('添加运费模板成功！','back_list', 'index.php?app=my_shipping');

			}



		}
	}

	/**
     *    编辑配送方式
     *
     *    @author    Garbin
     *    @return    void
     */
	function edit()
	{
		$shipping_id = isset($_GET['shipping_id']) ? intval($_GET['shipping_id']) : 0;
		if (!$shipping_id)
		{
			echo Lang::get('no_such_shipping');

			return;
		}

		/* 判断是否是自己的 */
		$model_shipping =& m('shipping');
		$shipping = $model_shipping->get("store_id=" . intval($this->visitor->get('manage_store')) . " AND shipping_id={$shipping_id}");
		if (!$shipping)
		{
			echo Lang::get('no_such_shipping');

			return;
		}
		
		if (!IS_POST)
		{
			/* 当前位置 */
			$this->_curlocal(LANG::get('seller_center'),    'index.php?app=seller',
			LANG::get('my_shipping'), 'index.php?app=my_shipping',
			LANG::get('edit_shipping'));

			/* 当前用户中心菜单 */
			$this->_curitem('my_shipping');

			/* 当前所处子菜单 */
			$this->_curmenu('edit_shipping');

			$this->_get_regions();

			
			
			$model_shipping_area  =& m('shippingarea');
			$shipping_area = $model_shipping_area->get_shipping_area_list($shipping_id);//地区运费模板
			$shipping_area_default = array_shift($shipping_area);//默认运费模板

			$this->assign('shipping', $shipping);
			$this->assign('shipping_area', $shipping_area);
			$this->assign('shipping_area_default', $shipping_area_default);
			//$this->assign('yes_or_no', array(1 => Lang::get('yes'), 0 => Lang::get('no')));
			$this->import_resource('mlselection.js, jquery.plugins/jquery.validate.js');
			header("Content-Type:text/html;charset=" . CHARSET);
			$this->display('my_shipping.edit.html');
		}
		else
		{
			
			//print_r($_POST);die();
			$data = array(
			//'store_id'      => intval($this->visitor->get('manage_store')),
			'shipping_name' => html_script($_POST['shipping_name']),
			'shipping_desc' => html_script($_POST['shipping_desc']),
			'cod_regions'   => html_script($_POST['region_name']),//发货地址
			'compute_mode'  => intval($_POST['sendType'])==0?'by_number':'by_weight',//按件算还是按重量算
			'item'          => intval($_POST['sendjs']),//默认运费多少件内
			'item_price'    => intval($_POST['sendy']),//默认运费多少件内多少钱
			'step'          => intval($_POST['sendj']),//增加多少
			'step_price'    => intval($_POST['sendyt']),//增加单位内多少钱
			'shipping_area'   => $_POST['logistics'],//地区运费
			//   'enabled'       => $_POST['enabled'],
			//  'sort_order'    => $_POST['sort_order'],
			);

		//	$shipping_data['store_id'] =  $data['store_id'];
			$shipping_data['shipping_name'] =  $data['shipping_name'];
			$shipping_data['shipping_desc'] =  $data['shipping_desc'];
			$shipping_data['cod_regions'] =  $data['cod_regions'];
			$shipping_data['compute_mode'] =  $data['compute_mode'];

			$model_shipping =& m('shipping');
			$model_shipping->edit($shipping_id, $shipping_data);
	
			if ($model_shipping->has_error())
			{
				//$this->show_warning($model_shipping->get_error());
				$msg = $model_shipping->get_error();
				$this->pop_warning($msg['msg']);
				return;
			}
			else
			{
				
				$model_shippingarea =& m('shippingarea');
				$model_shippingarea->drop(" shipping_id =" .$shipping_id);
				
				
				$shipping_area_id = $model_shippingarea->add($shipping_id,$data);
				
			}

			$this->show_message('编辑运费模板成功.','back_list', 'index.php?app=my_shipping');
		}
	}

	/**
     *    删除配送方式
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
	function drop()
	{
		$shipping_id = isset($_GET['shipping_id']) ? trim($_GET['shipping_id']) : 0;
		if (!$shipping_id)
		{
			$this->show_warning('no_such_shipping');

			return;
		}
		$ids = explode(',', $shipping_id);//获取一个类似array(1, 2, 3)的数组
		$model_shipping  =& m('shipping');
		$drop_count = $model_shipping->drop("store_id = " . $this->visitor->get('manage_store') . " AND shipping_id " . db_create_in($ids));
		if($drop_count)
		{
			$model_shipping_area  =& m('shippingarea');
			$model_shipping_area->drop(" shipping_id " . db_create_in($ids));
		}
		if (!$drop_count)
		{
			/* 没有可删除的项 */
			$this->show_warning('no_such_shipping');

			return;
		}

		if ($model_shipping->has_error())    //出错了
		{
			$this->show_warning($model_shipping->get_error());

			return;
		}

		$this->show_message('drop_shipping_successed');
	}

	/**
     *    三级菜单
     *
     *    @author    Garbin
     *    @return    void
     */
	function _get_member_submenu()
	{
		$menus = array(
		array(
		'name'  => 'shipping_list',
		'url'   => 'index.php?app=my_shipping',
		),
		/*            array(
		'name'  => 'add_shipping',
		'url'   => 'index.php?app=my_shipping&act=add',
		),*/
		);
		if (ACT == 'edit')
		{
			$menus[] = array(
			'name'  => 'edit_shipping',
			);
		}
		return $menus;
	}
	function _get_regions()
	{
		$model_region =& m('region');
		$regions = $model_region->get_list(0);
		if ($regions)
		{
			$tmp  = array();
			foreach ($regions as $key => $value)
			{
				$tmp[$key] = $value['region_name'];
			}
			$regions = $tmp;
		}
		$this->assign('regions', $regions);
	}
	function _assign_form()
	{
		/*赋初始值*/
		$shipping = array(
		'enabled'       => 1,
		'sort_order'    => 255,
		);
		$yes_or_no = array(
		1 => Lang::get('yes'),
		0 => Lang::get('no'),
		);
		$this->assign('yes_or_no', $yes_or_no);
		$this->assign('shipping' , $shipping);
	}
}

?>